﻿var WLL = {
	util :{}
};
WLL.util.form = {
	m : function(m, t, r){
		$.layer({
			rate: 'middle',
		    type: 0,
			title:false,
			area : ['auto','auto'],
		    shade: [0.1 , '#000' , true],
			end : function(){
				if(r != null){
					if(r == "goback"){
						window.history.back();
					}else{
						window.location.href = r;
					}
				}
			},
			dialog: {
				btns : 0,
				type : t,
				msg : m
			},
			closeBtn :false,
			time : 2
		});
	},
	msg : function(data){
		if(data.status == "success"){
			WLL.util.form.m(data.message, 1, data.url);
		}else if(data.status == "error"){
			WLL.util.form.m(data.message, 3, data.url);
		}else if(data.status == "warn"){
			WLL.util.form.m(data.message, 0, data.url);
		}
	},
	validate: function () {
        var $from = $("form.form-validate");
        var is_modal = $from.hasClass("form-modal");
        var modal = null;
        if (is_modal) {
            modal = $from.parents(".modal");
            modal.on('hidden', function () {
                $from.find("div.control-group").removeClass("error success");
                $from.find("span.error").removeClass("error").text("");
            });
        };
        if ($from.length > 0) {
            $from.each(function () {
                $(this).validate({
                    errorElement: "span",
                    errorClass: "help-block error",
                    ignore: ".ignore",
                    errorPlacement: function (e, t) {
                        var p = t.parents(".controls");
                        if (p.length > 0) {
                            p.append(e);
                            try{$("#step1").show();$("#step2").hide();}catch(e){};
                        } else {
                        	t.parents("dl").find(".error").html(e);
                        	try{error_index(t.attr("id").substring(t.attr("id").indexOf("_")+1));}catch(e){};
                        }
                    },
                    submitHandler: function (form) {
                    	var _sb = true;
                        if (typeof KindEditor !== "undefined" && KindEditor.instances) {
                            $.each(KindEditor.instances, function () {
                                this.sync();
                                var $element = $(this.srcElement[0]);
                                var i_ = $element.attr("id").replace("content_","");
                                if($("#title_"+i_).attr("disabled") != "disabled"){
	                                try{error_index($element.attr("id").replace("content_",""));}catch(e){}
	                                var r = $element.attr("data-rule-required"),
										m = $element.attr("data-msg-required"),
										e = $element.attr("data-rule-rangelength"),
										em = $element.attr("data-msg-rangelength");
	                                var v = $.trim($element.val()).replace(/(&nbsp;)|\s|\u00a0/g, '');
	                                if (r) {
	                                    if (v.length == 0) {
	                                        var msg = m;
	                                        WLL.util.msg.err(msg ? msg : "内容不能为空");
	                                        try{$(".baocun").html("保存");}catch(e){}
	                                        _sb = false;
	                                        return false
	                                    }
	                                };
	                                if (e) {
	                                    e = eval(e);
	                                    var min = e[0];
	                                    var max = e[1];
	                                    var vv = $element.val();
	                                    if (vv.length <= min || vv.length >= max) {
	                                        WLL.util.msg.err(em ? em : "内容不能小于{0}且大于{1}".format(min, max));
	                                        _sb = false;
	                                        try{$(".baocun").html("保存");}catch(e){}
	                                        return false;
	                                    }
	                                }
                                }
                            })
                        }
                        if(_sb){
                        	try{
                        		$(".ri").hide();
                        		$(".ri:first").show();
		                        var btn = $("button[type='submit']", form);
		                        btn.button('loading');
                        	}catch(e){}
	                        $(form).ajaxSubmit({
	                            dataType: 'json',
	                            success: function (d) {
	                            	try{
	                                    btn.button('reset');
                                	}catch(e){}
	                            	try{
	        	            			$(".baocun").html("保存");
                                	}catch(e){}
	                                if (modal) modal.modal('hide');
	                                WLL.util.form.msg(d);
	                                setInterval(function() {
		                                try{
		                                	var index = parent.layer.getFrameIndex(window.name);
		                    				parent.layer.close(index);
		                                }catch(e){}
	                        		}, 2000);
	                            },
	                            error: function (d) {
	                            	try{
	                                    btn.button('reset');
                                	}catch(e){}
	                            	try{
	        	            			$(".baocun").html("保存");
                                	}catch(e){}
	                                if (modal) modal.modal('hide');
	                                WLL.util.form.msg(d);
	                            }
	                        });
                        }
                    }
                });
            });
        }
    },
    init: function () {
        this.validate();
    }
};
WLL.util.msg = {
	base : function(m, t, r){
		$.layer({
			rate: 'middle',
		    type: 0,
			title:false,
			area : ['auto','auto'],
		    shade: [0.1 , '#000' , true],
			end : function(){if(r != null) window.location.href = r},
			dialog: {
				btns : 0,
				type : t,
				msg : m
			},
			closeBtn :false,
			time : 2
		});
	},
	base2 : function(m, t, f){
		$.layer({
			rate: 'middle',
		    type: 0,
			title:false,
			area : ['auto','auto'],
		    shade: [0.1 , '#000' , true],
			end : function(){if(f != null) f();},
			dialog: {
				btns : 0,
				type : t,
				msg : m
			},
			closeBtn :false,
			time : 2
		});
	},
	base2 : function(m, t, f,time){
		$.layer({
			rate: 'middle',
		    type: 0,
			title:false,
			area : ['auto','auto'],
		    shade: [0.1 , '#000' , true],
			end : function(){if(f != null) f();},
			dialog: {
				btns : 0,
				type : t,
				msg : m
			},
			closeBtn :false,
			time : time
		});
	},
	suc : function(m){
		WLL.util.form.m(m, 1);
	},
	err : function(m){
		WLL.util.form.m(m, 3);
	},
	warn : function(m){
		WLL.util.form.m(m, 0);
	}
}
WLL.util.confirm = {
	base : function(title, msg, t, okFun, noFun){
		var i = $.layer({
			title:title,
			area : ['auto','auto'],
		    shade: [0.1 , '#000' , true],
			dialog: {
				msg:msg,
		        btns : 2, 
		        type : t,
		        btn : ['确定','取消'],
		        yes : function(){
		        	okFun();
		        	layer.close(i);
		        },
		        no : function(){
		        	if(noFun != null) noFun();
		        	layer.close(i);
		        }
			},
			closeBtn :false
		});
	},
	del : function(okFun, noFun){
		this.base("删除提醒", "您确定删除吗？", 4, okFun, noFun);
	},
	del : function(okFun){
		this.base("删除提醒", "您确定删除吗？", 4, okFun, null);
	}
}
$(function () {
    WLL.util.form.init();
});
function setScale(value, scale, roundingMode) {
	if (roundingMode.toLowerCase() == "roundhalfup") {
		return (Math.round(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else if (roundingMode.toLowerCase() == "roundup") {
		return (Math.ceil(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	} else {
		return (Math.floor(value * Math.pow(10, scale)) / Math.pow(10, scale)).toFixed(scale);
	}
}
//货币格式化
function currencyFormat(price) {
	price = setScale(price, 2, "roundHalfUp");
	return "¥" + price + "元";
}